//
//  ModifyPersonalInformationViewController.m
//  1243
//
//  Created by ym on 16/5/9.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "ModifyPersonalInformationViewController.h"
#import "UserPhotoTableViewCell.h"
#import "UserInformationTableViewCell.h"
#import "ReadLoginData.h"
#import "ErWeiMaViewController.h"
#import "HomeViewController.h"
#import "MeViewController.h"
#import "NetWorkTools.h"
#import "VIPReginViewController.h"
#import "PasswordTableViewController.h"
#import "ErWEeiMaTuiJianViewController.h"
#import "ZhuCeViewController.h"
#define WIDTH [[UIScreen mainScreen] bounds].size.width
#define ANORMWIDTH [[UIScreen mainScreen] bounds].size.width / 414
#define HEIGHT [[UIScreen mainScreen] bounds].size.height
#define ANORMHEIGHT [[UIScreen mainScreen] bounds].size.height / 736
#define FRAME [[UIScreen mainScreen] bounds]
@interface ModifyPersonalInformationViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, copy) NSString *loginUsername;
@property (nonatomic, strong) UIImage *QRImage;
@property (nonatomic,strong) NSDictionary *loadUserData;
@end

@implementation ModifyPersonalInformationViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    NSDictionary *dict = @{
                           @"PhoneNum":[ReadLoginData readLoginData][@"UserName"],
                           @"Password":[ReadLoginData readLoginData][@"Password"]
                           };
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"USERLOGIN" completeBlock:^(id object) {
        
        NSDictionary *loginDict = [NetWorkTools dictionaryWithJsonString:[object objectForKey:@"BackData"]];
        [self cleanloginData];
        [ReadLoginData savelogindataToLocal:loginDict];
        
        self.loadUserData = [ReadLoginData readLoginData];

        NSLog(@"%@", self.loadUserData);
        [self.tableView reloadData];
        

    }];
    
    
    
    
    
    NSDictionary *dictA  = [ReadLoginData readLoginData];
    
    if (dictA.count) {
        // 已登录
        
    }else{
        
        
        
[[NSNotificationCenter defaultCenter] postNotificationName:@"DidClickMEMEUButtonChangeTabBarControllerSelected" object:nil userInfo:nil];
        
    }
    
}
- (NSDictionary *)loadUserData {
    if (!_loadUserData) {
        _loadUserData = [NSDictionary dictionary];
    }
    return _loadUserData;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self creatErweim];
    [self creatView];
    self.title = @"用户信息";
}
- (void)creatErweim{
    NSString *str = [NSString stringWithFormat:@"%@", [ ReadLoginData readLoginData][@"Number"]];
    self.loginUsername = str;
    NSString *QRImageName = [NSString stringWithFormat:@"%@%@",str,@".2"];
    NSData *stringData = [QRImageName dataUsingEncoding:NSUTF8StringEncoding];
    CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    //设置内容和纠错级别
    [qrFilter setValue:stringData forKey:@"inputMessage"];
    [qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"];
    
    UIColor *onColor = [UIColor blackColor];
    UIColor *offColor = [UIColor whiteColor];
    
    CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor"
                                       keysAndValues:
                             @"inputImage",qrFilter.outputImage,
                             @"inputColor0",[CIColor colorWithCGColor:onColor.CGColor],
                             @"inputColor1",[CIColor colorWithCGColor:offColor.CGColor],
                             nil];
    
    CIImage *qrImage = colorFilter.outputImage;
    
    CGSize size = CGSizeMake(300, 300);
    CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:qrImage fromRect:qrImage.extent];
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetInterpolationQuality(context, kCGInterpolationNone);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
    UIImage *codeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGImageRelease(cgImage);
    _QRImage = codeImage;
    [self.tableView reloadData];
}

- (void)creatView{
    self.tableView = [[UITableView alloc] initWithFrame: CGRectMake(0, 0, WIDTH,HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"UserPhotoTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"UserPhotoTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"UserInformationTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"UserInformationTableViewCell"];
    self.tableView.scrollEnabled =NO; //设置tableview 不能滚动
    
}

- (void)cleanloginData {
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
    //    NSLog(@"path = %@",path);
    NSString *filename=[path stringByAppendingPathComponent:@"loginData.plist"];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dic writeToFile:filename atomically:YES];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    if (indexPath.row == 0 || indexPath.row == 3) {
        UserPhotoTableViewCell *photoCell = [tableView dequeueReusableCellWithIdentifier:@"UserPhotoTableViewCell"];
        photoCell.selectionStyle = UITableViewCellSelectionStyleNone;

        if (indexPath.row == 3) {
            photoCell.userName.text  = @"二维码名片";
            photoCell.userImage.image = _QRImage;
            
        }else {
            
            photoCell.userName.text = @"头像";
            photoCell.userImage.image = [UIImage imageNamed:@"WeChat_1464149154-1"];
            photoCell.userImage.contentMode = UIViewContentModeScaleAspectFit;
            photoCell.userImage.layer.cornerRadius = photoCell.userImage.width/2;
            photoCell.userImage.layer.masksToBounds = YES;
        }
        
        
        
        return photoCell;
    }else if(indexPath.row == 1 || indexPath.row == 2){
        UserInformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UserInformationTableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row == 2) {
            cell.userInfor.text = @"性别";
            cell.userXXXX.text = @"男";
        }else{
            cell.userXXXX.text  =  [self.loadUserData objectForKey:@"RealName"];
            
        }

        return cell;
        
        
    }else if(indexPath.row == 1 || indexPath.row == 4){
        
        static NSString *identifier=@"mimacell";
        
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
        
        if (cell==nil) {
            cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text= @"密码设置";
        cell.textLabel.font = [UIFont systemFontOfSize:17];
        cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 5);
        
        return cell;
    }else{
        static NSString *identifier=@"wanshancell";
        
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
        
        if (cell==nil) {
            cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text= @"完善用户信息";
        cell.textLabel.font = [UIFont systemFontOfSize:17];
        cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 5);
        
        return cell;
    }
    
    
    
    
}


- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section
{
    return 60;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView = [[UIView alloc] init];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTintColor:[UIColor whiteColor]];
    [button setTitle:@"退出" forState:UIControlStateNormal];
    //    [button setBackgroundColor:NAVBAR_COLOR];
    [button setBackgroundColor:[UIColor orangeColor]];
    button.frame = CGRectMake(10, 10, WEITH-20, 40);
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    [footView addSubview:button];
    [button addTarget:self action:@selector(returnBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    return footView;
}

- (void)returnBtnClick
{
    [self cleanloginData];
    [ReadLoginData removeDataToLocal:@"addressData.plist"];
    [ReadLoginData removeDataToLocal:@"shopData.plist"];
    [self.tableView reloadData];
    // [self.navigationController popViewControllerAnimated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"DidClickTUICHUButtonChangeTabBarControllerSelected" object:nil userInfo:nil];
}


- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
        NSDictionary *dictA  = [ReadLoginData readLoginData];
    
    if (dictA.count) {
        // 已登录
        
    }else{
        
        [self.navigationController popViewControllerAnimated:YES];

    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 3) {
        ErWEeiMaTuiJianViewController *a = [[ErWEeiMaTuiJianViewController alloc] init];
       [self pushForm:self.navigationController pushToAndHidden:a];

    }else if (indexPath.row == 4){
        
        PasswordTableViewController *pass = [[PasswordTableViewController alloc] init];
        [self pushForm:self.navigationController pushToAndHidden:pass];
    }else if (indexPath.row == 5){
        ZhuCeViewController *reg = [[ZhuCeViewController alloc] init];
        [self pushForm:self.navigationController pushToAndHidden:reg];
        
    }
    
    
}
- (void)pushForm:(UINavigationController *)pushForm pushToAndHidden:(UIViewController *)pushToVC
{
    [pushToVC setHidesBottomBarWhenPushed:YES];
    [pushForm pushViewController:pushToVC animated:YES];
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}



@end
